#include <iostream>
#include <string>
#include "Student.h"
#include <fstream> //c++文件流头文件
using namespace std;

extern vector<Student> students;      // 全局变量在main文件中声明，在task2文件中赋值
extern vector<Car> cars;              // 全局变量在main文件中声明，在task1文件中赋值
extern vector<Student> students_duqu; // 任务4中读取文件，将其信息存储在students_duqu中,在main文件中声明，在task4文件中赋值

// 4.1 读取文件并将学生及其小车的信息存储到students_duqu中
void readStudentCarInfoFromFile(const string &filename)
{
    ifstream file(filename);
    if (!file.is_open())
    {
        cerr << "无法打开文件 " << filename << endl;
        return;
    }

    string line;
    Student student;
    bool capture = false;

    while (getline(file, line))
    {
        if (line.find("学生学号:") != string::npos)
        {
            if (capture)
            {
                students_duqu.push_back(student);
            }
            capture = true;
            string ss(line.substr(line.find(":") + 2));
            student.id = ss;
        }
        else if (line.find("学生姓名:") != string::npos && capture)
        {
            string ss(line.substr(line.find(":") + 2));
            student.name = ss;
        }
        else if (line.find("被分配的小车编号:") != string::npos && capture)
        {
            string ss(line.substr(line.find(":") + 2));
            student.car.id = ss;
        }
        else if (line.find("底盘编号:") != string::npos && capture)
        {
            string ss(line.substr(line.find(":") + 2));
            student.car.underpan.id = ss;
        }
        else if (line.find("底盘型号:") != string::npos && capture)
        {
            string ss(line.substr(line.find(":") + 2));
            student.car.underpan.model = ss;
        }
        else if (line.find("轴距:") != string::npos && capture)
        {
            string ss(line.substr(line.find(":") + 2));
            student.car.underpan.wheelbase = stoi(ss); // string转int类型
        }
        else if (line.find("轮距:") != string::npos && capture)
        {
            string ss(line.substr(line.find(":") + 2));
            student.car.underpan.track_width = stoi(ss);
        }
        else if (line.find("最小离地间隙:") != string::npos && capture)
        {
            string ss(line.substr(line.find(":") + 2));
            student.car.underpan.ground_clearance = stoi(ss);
        }
        else if (line.find("最小转弯半径:") != string::npos && capture)
        {
            string ss(line.substr(line.find(":") + 2));
            student.car.underpan.turning_radius = stoi(ss);
        }
        else if (line.find("驱动形式:") != string::npos && capture)
        {
            string ss(line.substr(line.find(":") + 2));
            student.car.underpan.drive_form = ss;
        }
        else if (line.find("最大行程:") != string::npos && capture)
        {
            string ss(line.substr(line.find(":") + 2));
            student.car.underpan.max_range = stoi(ss);
        }
        else if (line.find("轮胎型号:") != string::npos && capture)
        {
            // 轮胎信息需要特别处理，因为每台车有四个轮胎
            size_t start_pos = line.find(":") + 2;              //找到冒号（:）的位置，并设置start_pos为冒号后两个字符的位置（跳过冒号和空格）
            size_t comma_pos = line.find(", 尺寸:", start_pos); //从start_pos位置开始查找下一个逗号和随后的"尺寸:"字符串
            string tire_type = line.substr(start_pos, comma_pos - start_pos);
            string tire_size = line.substr(comma_pos + 7);
            student.car.underpan.tires.push_back(Tire(tire_type, tire_size));
        }
        else if (line.find("AGX套件型号:") != string::npos && capture)
        {
            string ss(line.substr(line.find(":") + 2));
            student.car.agx.model = ss;
        }
        else if (line.find("AI性能:") != string::npos && capture)
        {
            string ss(line.substr(line.find(":") + 2));
            student.car.agx.aiPerformance = stoi(ss);
        }
        else if (line.find("CUDA核心数:") != string::npos && capture)
        {
            string ss(line.substr(line.find(":") + 2));
            student.car.agx.cudaCores = stoi(ss);
        }
        else if (line.find("Tensor核心数:") != string::npos && capture)
        {
            string ss(line.substr(line.find(":") + 2));
            student.car.agx.tensorCores = stoi(ss);
        }
        else if (line.find("显存:") != string::npos && capture)
        {
            string ss(line.substr(line.find(":") + 2));
            student.car.agx.memory = stoi(ss);
        }
        else if (line.find("存储:") != string::npos && capture)
        {
            string ss(line.substr(line.find(":") + 2));
            student.car.agx.storage = stoi(ss);
        }
        else if (line.find("双目摄像头型号:") != string::npos && capture)
        {
            string ss(line.substr(line.find(":") + 2));
            student.car.camera.model = ss;
        }
        else if (line.find("RGB帧分辨率宽度:") != string::npos && capture)
        {
            string ss(line.substr(line.find(":") + 2));
            student.car.camera.rgbResolutionWidth = stoi(ss);
        }
        else if (line.find("RGB帧分辨率高度:") != string::npos && capture)
        {
            string ss(line.substr(line.find(":") + 2));
            student.car.camera.rgbResolutionHeight = stoi(ss);
        }
        else if (line.find("RGB帧率:") != string::npos && capture)
        {
            string ss(line.substr(line.find(":") + 2));
            student.car.camera.rgbFrameRate = stoi(ss);
        }
        else if (line.find("水平视场角:") != string::npos && capture)
        {
            string ss(line.substr(line.find(":") + 2));
            student.car.camera.fovHorizontal = stoi(ss);
        }
        else if (line.find("垂直视场角:") != string::npos && capture)
        {
            string ss(line.substr(line.find(":") + 2));
            student.car.camera.fovVertical = stoi(ss);
        }
        else if (line.find("深度帧率:") != string::npos && capture)
        {
            string ss(line.substr(line.find(":") + 2));
            student.car.camera.depthFrameRate = stoi(ss);
        }
        else if (line.find("多线激光雷达型号:") != string::npos && capture)
        {
            string ss(line.substr(line.find(":") + 2));
            student.car.lidar.model = ss;
        }
        else if (line.find("通道数:") != string::npos && capture)
        {
            string ss(line.substr(line.find(":") + 2));
            student.car.lidar.channels = stoi(ss);
        }
        else if (line.find("测试范围:") != string::npos && capture)
        {
            string ss(line.substr(line.find(":") + 2));
            student.car.lidar.range = stof(ss);
        }
        else if (line.find("功耗:") != string::npos && capture)
        {
            string ss(line.substr(line.find(":") + 2));
            student.car.lidar.power = stof(ss);
        }
        else if (line.find("9轴陀螺仪型号:") != string::npos && capture)
        {
            size_t start_pos = line.find(":") + 2;              //找到冒号（:）的位置，并设置start_pos为冒号后两个字符的位置（跳过冒号和空格）
            size_t comma_pos = line.find(", 厂家:", start_pos); //从start_pos位置开始查找下一个逗号和随后的"尺寸:"字符串
            string gyroscope_model = line.substr(start_pos, comma_pos - start_pos);
            string gyroscope_manufacturer = line.substr(comma_pos + 7);
            student.car.gyroscope.model = gyroscope_model;
            student.car.gyroscope.manufacturer = gyroscope_manufacturer;
        }
        else if (line.find("液晶显示屏尺寸:") != string::npos && capture)
        {
            string ss(line.substr(line.find(":") + 2));
            student.car.green.size = stof(ss);
        }
        else if (line.find("液晶显示屏型号:") != string::npos && capture)
        {
            string ss(line.substr(line.find(":") + 2));
            student.car.green.model = ss;
        }
        else if (line.find("电池电压:") != string::npos && capture)
        {
            string ss(line.substr(line.find(":") + 2));
            student.car.battery.voltage = ss;
        }
        else if (line.find("电池容量:") != string::npos && capture)
        {
            string ss(line.substr(line.find(":") + 2));
            student.car.battery.capacity = ss;
        }
        else if (line.find("对外供电电压:") != string::npos && capture)
        {
            string ss(line.substr(line.find(":") + 2));
            student.car.battery.supplyVoltage = ss;
        }
        else if (line.find("充电时长:") != string::npos && capture)
        {
            string ss(line.substr(line.find(":") + 2));
            student.car.battery.chargingTime = stof(ss);
        }
        else if (line.find("----------------------------------------") != string::npos)
        {
            if (capture)
            {
                students_duqu.push_back(student);
                student = Student(); // 重置学生信息为下一个学生做准备
            }
            capture = false; // 停止捕获信息，直到下一个学生学号出现
        }
    }

    if (capture)
    {
        // 确保文件中最后一个学生的信息也被添加
        students_duqu.push_back(student);
    }

    file.close();
}

// 4.2 将相关信息显示在屏幕上（含分配的学生信息）
void task4_display_info()
{
    cout << "读取出的文件相关信息如下:" << endl;
    for (Student &students : students_duqu)
    {
        cout << "学生学号：" << students.id << "  学生姓名：" << students.name << " 该学生分配的小车信息如下：" << endl;
        students.car.printCarDetails();
    }
}

// 4.3 一次显示1辆小车信息，按n键显示下一辆小车信息，按p显示上一辆小车信息。请注意：显示第1台显示按p无效，显示第10台小车，按n无效。
void task4()
{
    if (students_duqu.empty())
    {
        cout << "没有学生信息。" << endl;
        return;
    }

    int current = 0; // 当前显示的学生索引
    int size = students_duqu.size();

    char command;
    do
    {
        cout << "当前显示的学生信息：" << endl;
        cout << "学生学号：" << students_duqu[current].id
             << "  学生姓名：" << students_duqu[current].name << endl;
        students_duqu[current].car.printCarDetails();

        cout << "按 'n' 显示下一辆小车信息，按 'p' 显示上一辆小车信息，按其他键退出：";
        cin >> command;
        // cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 忽略剩余的换行符

        if (command == 'n' && current < size - 1)
        {
            current++;
        }
        else if (command == 'p' && current > 0)
        {
            current--;
        }
        else if (command != 'n' && command != 'p')
        {
            break; // 退出循环
        }

        // 显示提示信息
        if (current == 0 && command == 'p')
        {
            cout<< endl<< "这已经是第一辆小车了，无法显示上一辆！！！！！！！！！！！！！！！！" << endl<<endl;
        }
        else if (current == size - 1 && command == 'n')
        {
            cout<<endl<< "这已经是最后一辆小车了，无法显示下一辆！！！！！！！！！！！！！！！！" << endl<<endl;
        }
    } while (command == 'n' || command == 'p');

    cout << "退出显示。" << endl;
}